package org.dromara.genealogy.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.genealogy.domain.vo.GeGeneraVo;
import org.dromara.genealogy.domain.bo.GeGeneraBo;
import org.dromara.genealogy.service.IGeGeneraService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 世代
 *
 * @author qianjm
 * @date 2025-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/genealogy/genera")
public class GeGeneraController extends BaseController {

    private final IGeGeneraService geGeneraService;

    /**
     * 查询世代列表
     */
    @SaCheckPermission("genealogy:genera:list")
    @GetMapping("/list")
    public TableDataInfo<GeGeneraVo> list(GeGeneraBo bo, PageQuery pageQuery) {
        return geGeneraService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出世代列表
     */
    @SaCheckPermission("genealogy:genera:export")
    @Log(title = "世代", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GeGeneraBo bo, HttpServletResponse response) {
        List<GeGeneraVo> list = geGeneraService.queryList(bo);
        ExcelUtil.exportExcel(list, "世代", GeGeneraVo.class, response);
    }

    /**
     * 获取世代详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("genealogy:genera:query")
    @GetMapping("/{id}")
    public R<GeGeneraVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(geGeneraService.queryById(id));
    }

    /**
     * 新增世代
     */
    @SaCheckPermission("genealogy:genera:add")
    @Log(title = "世代", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GeGeneraBo bo) {
        return toAjax(geGeneraService.insertByBo(bo));
    }

    /**
     * 修改世代
     */
    @SaCheckPermission("genealogy:genera:edit")
    @Log(title = "世代", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GeGeneraBo bo) {
        return toAjax(geGeneraService.updateByBo(bo));
    }

    /**
     * 删除世代
     *
     * @param ids 主键串
     */
    @SaCheckPermission("genealogy:genera:remove")
    @Log(title = "世代", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(geGeneraService.deleteWithValidByIds(List.of(ids), true));
    }
}
